/**
 * @description 活动开始提醒通知订阅消息模板id
 */
export const RECEIVED_PLACE_ORDER_TPL_ID = 'BuFgfs-iqbphWXXRizj-pF7r52Ax4K4C7aoc4qjgaWI'


/**
 * @description 活动抢购成功提醒 订阅消息模板id
 */
export const RECEIVED_PLACE_ORDER_WIN_ID = 'nWnUmwk5999CwI_PzI2fXZZWsPXPK6yJbGItjPa15is'


/**
 * @description 核销订单 订阅消息模板id
 */
export const CANCEL_AN_ORDER_ID = 'OTOaBKpkt9iPO4UDlvVY28CxL1kTi8oEV6YudfTxKPU'


/**
 * @description 会员消费成功通知 订阅消息模板id
 */
export const MEMBER_CONSUMPTION_ID = 'KZmBEitVHvtBts8XBJLqPGj722rdKflQMmE7M7ZoMgQ'
/**
 * @description 订阅微信小程序消息模板
 * @param {Array} tplIds 订阅消息模板id，['xxxx', 'xxxxxx']
 */
export const subscribeMessage = (tplIds) => {
    uni.showLoading({title:'请稍后...'})
    return new Promise((resolve, reject) => {
        uni.getSetting({
            withSubscriptions: true, // 是否获取用户订阅消息的订阅状态，默认false不返回
            success: (res) => {
                console.log('getSetting res', res)
                if (res.subscriptionsSetting.mainSwitch) { //用户打开了订阅消息总开关
                    // console.log('进来了',res.subscriptionsSetting.itemSettings)
                    if (res.subscriptionsSetting.itemSettings != null) { //用户同意总是保持是否推送消息的选择, 这里表示以后不会再拉起推送消息的授权
                        let lettingsList = res.subscriptionsSetting
                        let tmplIds = []
                        tplIds.forEach(item => {
                            if (!lettingsList.itemSettings[item]) {
                                tmplIds.push(item)
                            } else if (lettingsList.itemSettings[item] && lettingsList.itemSettings[item] === 'accept') {
                                tmplIds.push(item)
                            }
                        })
                        if (tmplIds.length) {
                            uni.requestSubscribeMessage({
                                tmplIds: tmplIds,
                                success: () => {
									console.log('成功111')
                                    resolve()
                                },
                                fail: (res) => {
                                    console.log('tpl subscribe fail', res)
                                    resolve()
                                }
                            })
                        } else {
                            resolve()
                        }
                    } else {
                        uni.requestSubscribeMessage({
                            tmplIds: tplIds,
                            success: () => {
								console.log('成功222')
                                resolve()
                            },
                            fail: (res) => {
                                console.log('tpl subscribe fail', res)
                                resolve()
                            }
                        })
                    }
                } else {
                   resolve() 
                }
            },
            fail() {
                resolve()
            },
            complete() {
                uni.hideLoading()
            }
        })
    })
}